package com.multiplayer.dao.impl;

import java.sql.Connection;
import java.util.List;

import com.multiplayer.dao.IAdminDao;
import com.multiplayer.entity.Admin;
import com.multiplayer.entity.Emp;
import com.multiplayer.tools.DBTools;
import com.multiplayer.tools.ITransaction;
import com.multiplayer.tools.PageData;
/**
 * 
 * @author 温在鑫
 *@version   [版本号, 2020-05-08]
 *管理员操作实现类
 */
public class AdminDaoImpl implements IAdminDao{

	@Override
	public boolean addAdmin(Admin admin) {
		String sql = "insert into admin (ano, empno, apassword) values(?,?,?)";
		int result = (Integer)DBTools.transaction(new ITransaction() {
			
			@Override
			public Object execute(Connection conn) {
				int n1 = 0;
				try {
					n1 = DBTools.exUpdate(sql, conn, admin.getAno(),admin.getEmpno(),admin.getApassword());
				} catch (Exception e) {
					// TODO Auto-generated catch block
					throw new RuntimeException("存储失败，事务回滚!", e);
				}
				return n1;
			}
		});
		
		return result>0;
	}

	@Override
	public boolean deleteAdminById(String adminId) {
		String sql = "delete from admin where ano = ?";
		int result = (Integer)DBTools.transaction(new ITransaction() {
			
			@Override
			public Object execute(Connection conn) {
				int n1 = 0;
				try {
					n1 = DBTools.exUpdate(sql, conn, adminId);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					throw new RuntimeException("删除失败，事务回滚!", e);
				}
				
				return n1;
			}
		});
		return result>0;
	}

	@Override
	public boolean editAdmin(Admin admin) {
		String sql = "update admin set apassword=? where ano = ?";
		int result = (Integer)DBTools.transaction(new ITransaction() {
			
			@Override
			public Object execute(Connection conn) {
				int n1 = 0;
				try {
					n1 = DBTools.exUpdate(sql, conn, admin.getApassword(),admin.getAno());
				} catch (Exception e) {
					// TODO Auto-generated catch block
					throw new RuntimeException("修改失败，事务回滚!", e);
				}
				return n1;
			}
		});
		return result>0;
	}

	@Override
	public List<Admin> queryAdminByKeywords(String keywords) {
		String sql = "select ano, empno, apassword from admin where ano like ? or empno like ?";
		List<Admin> list = (List<Admin>)DBTools.exQuery(sql, Admin.class, "%"+keywords+"%","%"+keywords+"%");
		
		return list;
	}

	@Override
	public List<Admin> queryAdminById(String adminId) {
		String sql = "select ano, empno, apassword from admin where ano = ?";
		List<Admin> list = (List<Admin>)DBTools.exQuery(sql, Admin.class,adminId);
		return list;
	}

	@Override
	public PageData queryAdminPageData(Integer page, Integer pageSize) {
		String sql = "select ano, empno, apassword from admin";
		PageData pageData = DBTools.exQueryByPage(sql, Admin.class, page, pageSize);
		return pageData;
	}

	@Override
	public List<Emp> queryEmp(String empno) {
		String sql = "select empno, empname, empsex, empage from emp where empno = ?";
		List<Emp> list = (List<Emp>)DBTools.exQuery(sql, Emp.class, empno);
		return list;
	}

}
